﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using System.Data;
using Microsoft.Reporting.WebForms;

namespace Catering.Catering_Page.richang
{
    public partial class rbtj : UserIsNull
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.txt_StartTime.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy'})");
                this.txt_EndTime.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd'})");
                loadPage();
            }
        }

        private void loadPage()
        {
            Bind_Data_to_Controls.Bind_Area_ddl(ddl_Area, Base_IsAdmin, Base_Groupid, Base_Companyid);
            Bind_Data_to_Controls.Bind_Class_ddl(ddl_Area.SelectedItem, ddl_class, Base_IsAdmin, Base_Classid);
        }

        protected void ddl_Area_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl_Area.SelectedItem != null)
            {
                Bind_Data_to_Controls.Bind_Class_ddl(ddl_Area.SelectedItem, ddl_class);
            }
        }

        protected void Dr_DateType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Dr_DateType.SelectedItem != null)
            {
                if (Dr_DateType.SelectedItem.Value.Equals("Year"))
                {
                    this.txt_StartTime.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy'})");
                    this.txt_StartTime.Text = "";
                    this.othertype01.Visible = false;
                    this.othertype02.Visible = false;
                }
                else if (Dr_DateType.SelectedItem.Value.Equals("Month"))
                {
                    this.txt_StartTime.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-M'})");
                    this.txt_StartTime.Text = "";
                    this.othertype01.Visible = false;
                    this.othertype02.Visible = false;
                }
                else
                {
                    this.txt_StartTime.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd'})");
                    this.txt_StartTime.Text = "";
                    this.othertype01.Visible = true;
                    this.othertype02.Visible = true;
                }
            }
        }
        /// <summary>
        /// 开始统计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string groupid = this.ddl_Area.SelectedValue;         //作业区
            string classid = this.ddl_class.SelectedValue;        //班组
            DateTime StartDate = DateTime.MinValue;               //起始时间
            DateTime EndDate = DateTime.MinValue;               //结束时间
            string dateType = this.Dr_DateType.SelectedValue;     //时间类型
            DateTime.TryParse(this.txt_StartTime.Text, out StartDate);
            DateTime.TryParse(this.txt_EndTime.Text, out EndDate);
            if (dateType == "Year")
            {
                DateTime.TryParse(this.txt_StartTime.Text + "-01", out StartDate);
            }
            if (StartDate.Year < 1949 || (dateType == "Other" && StartDate >= EndDate)) return;
            this.Lbl_Title.Text = GetTitle(StartDate, EndDate, dateType);
            //开始查询
            DataTable dt = new View_Monitor().GetRbaoTongji(Base_Companyid.ToString(), groupid, classid, StartDate, EndDate, dateType, false);
            ReportDataSource rds = new ReportDataSource("DataSet1", dt);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rds);
            ReportViewer1.LocalReport.Refresh();
            ReportViewer1.Visible = true;
        }

        /// <summary>
        /// 获取标题
        /// </summary>
        /// <param name="StartDate"></param>
        /// <param name="EndDate"></param>
        /// <param name="dateType"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        private string GetTitle(DateTime StartDate, DateTime EndDate, string dateType)
        {
            string title = "";
            if (dateType == "Year")
            {
                title += this.txt_StartTime.Text + "年";
            }
            else if (dateType == "Month")
            {
                title += StartDate.ToString("yyyy年MM月");
            }
            else
            {
                title += StartDate.ToString("yyyy年MM月dd日") + "到" + EndDate.ToString("yyyy年MM月dd日");
            }
            if (this.ddl_Area.SelectedItem.Text != "全部")
            {
                title += this.ddl_Area.SelectedItem.Text;
                if (this.ddl_class.SelectedItem.Text != "全部") title += this.ddl_class.SelectedItem.Text;
            }
            else
            {
                title += "所有作业区";
            }
            title += "日报统计";
            return title;
        }
    }
}